/*	Some functions you'd commonly use in form-checking.
	Martin Warin, Jan 24 2008
*/

function getOptionText(control) {
	if (control.selectedIndex < 0) {
		return undefined;
	}
	else {
		return control.options[control.selectedIndex].text;
	}
}

function getOptionValue(control) {
	if (control.selectedIndex < 0) {
		return undefined;
	}
	else {
		return control.options[control.selectedIndex].value;
	}
}

// saves keystrokes
function get(id){
	return document.getElementById(id);
}

/* 	saves keystrokes, e.g:
 	if(!emailChecker(get('email'))){
		alert(getV('email') + " is not a valid email address.");
		return false;
	}
	compare:
		alert(document.getElementById('email').value + " is not a valid email address.");
*/
function getV(id){
	return get(id).value;
}

/* 	saves keystrokes, e.g: 
	if(getL('userFN') >= 50){
		return false;
	}
	compare: 
	if(document.getElementById('userFN').value.length >= 50){ ...
*/
function getL(id){
	return getV(id).length;
}

// saves keystrokes when you wanna create a new dom node.
// e.g. var foo = make('select', 'chooseRole');
// or get('myDiv').appendChild(make('br', ''));
function make(type, nameId){
	var e 	= document.createElement(type);
	if(nameId != ""){
		e.name	= nameId;
		e.id 	= nameId;
	}
	return e;
}

/*	validates email address
	e.g., in your formcheck function, you could do:
	if(!emailChecker(get('email'))){
		return false;
	}
*/
function emailChecker(email){
	if( email.value == "" 
		|| !email.value.match(/^.+@.+\..+$/) 
		|| email.value.match(/[^a-zA-Z0-9\.\-_@]/) ){
		alert("Please enter a valid email address.");
		email.select();
		email.focus();
		return false;
	}
	return true;
}

// textarea maxlength
// e.g. <textarea ... onKeyPress="return checkLength(event, this.id, 5);"></textarea>
function checkLength(e, id, maxLen){
	if(e.keyCode == 13 || e.charCode != 0){ // only applies to real char-events and ENTER.
		if(get(id).value.length >= maxLen){
			alert("The maximum length for this field is " +maxLen+ " characters.");
			return false;
		}
	}
	return true;
}


 //string trim function
 String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g,"");
	} 
